<template>
  <div class="images">
    <div class="image-add" v-for="(image, index) in images">
      <div class="-inner">
        <img :src="image" class="image" />
        <a class="del-ops" @click.stop="deleteResource(index)"
          ><i class="el-icon-remove"></i
        ></a>
      </div>
    </div>
    <div class="image-add">
      <div class="-inner">
        <a @click.stop="selectResource"
          ><i class="el-icon-plus avatar-uploader-icon"></i
        ></a>
      </div>
    </div>
    <resource-select
      :visible.sync="resourceDialog.visible"
      :multipled="true"
      @update:selected="doSelectResource"
    ></resource-select>
  </div>
</template>

<script>
import ResourceSelect from "./resourceSelect";

export default {
  components: { ResourceSelect },
  name: "swak-images",
  data() {
    return {
      resourceDialog: {
        visible: false
      },
      images: []
    };
  },
  props: {
    value: Array
  },
  watch: {
    value(newValue) {
      this.images = newValue || [];
    },
    images(newValue) {
      this.$emit("input", newValue);
    }
  },
  methods: {
    selectResource: function() {
      this.resourceDialog.visible = true;
    },
    doSelectResource: function(resources) {
      resources.forEach(r => {
        this.images.push(r.url);
      });
    },
    deleteResource: function(index) {
      this.images.splice(index, 1);
    }
  }
};
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
@require "../../common/stylus/variable.styl"
.image-add {
  margin-right 10px;
  .-inner {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    display inline-block;
    i {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .image {
      width: 178px;
      height: 178px;
      display block;
    }
    &:hover {
      border-color: #409EFF;
      i {
        color: #409EFF;
      }
      .del-ops {
        display block;
      }
    }
    .del-ops {
      position absolute;
      left 0;
      top 0;
      right 0;
      height 0;
      display none;
    }
  }
}

.images {
  .image-add {
    float left;
  }
}
</style>
